<?php
class tourism_order_product_profit_model extends CI_Model
{
    private $table = 'tourism_order_product_profit';
    private $table_2 = 'tourism_order';
    private $table_3 = 'tourism_order_product';
    private $table_4 = 'tourism_order_refund';
    
    public function insertBatch($postData){
        if (empty($postData) ){
            return false;
        }

        foreach ($postData as $key=>$value) {
            if (empty($value)) {
                return false;
            }
            if(is_array($value)){
                foreach ($value as $v) {
                    if($v['account'] > 0){
                        $data[] = array(
                            'order_product_id' => $key,
                            'uid'              => $v['uid'],
                            'account'          => $v['account'],
                            'account_type'     => $v['account_type'],
                            'as'               => $v['as'],
                            'bit_trade'        => $v['bit_trade']
                        );
                    }
                }
            }
        }
        $result = true;
        if(!empty($data)){
            $result = $this->db->insert_batch($this->table, $data);
        }
        return $result;
    }
    
    /**
     * @descripte   分润
     * @Author xiumao
     * @date 2016/6/28 0028 下午 3:41
     */
    public function profit_total($params = array())
    {
        $this->db->join($this->table_3 . ' as tourism_order_product', 'tourism_order_product.order_product_id = tourism_order_product_profit.order_product_id');
        $this->db->join($this->table_2 . ' as tourism_order', 'tourism_order.order_id = tourism_order_product.order_id');
        $this->db->join($this->table_4 . ' as tourism_order_refund', 'tourism_order_refund.order_product_id = tourism_order_product_profit.order_product_id', 'left');

        if ( !empty($params['uid']) ) {
            $this->db->where('tourism_order_product_profit.uid', $params['uid']);
        }
        if ( !empty($params['payer_uid']) ) {
            $this->db->where('tourism_order.payer_uid', $params['payer_uid']);
        }
        if ( !empty($params['order_id']) ) {
            $this->db->where('tourism_order.order_id', $params['order_id']);
        }
        if ( !empty($params['startTime']) ) {
            $this->db->where('tourism_order.created_at >=', $params['startTime'] . ' 00:00:00');
        }
        if ( !empty($params['endTime']) ) {
            $this->db->where('tourism_order.created_at <=', $params['endTime'] . ' 23:59:59');
        }
        if ( !empty($params['status']) && $params['status'] == 2 ) {
            $this->db->where("tourism_order.order_state", 4);
            $this->db->where("tourism_order.order_status", 6);
        } else if ( !empty($params['status']) && $params['status'] == 1 ) {
            $this->db->where("tourism_order.order_status !=", 6);
        }
        $this->db->where("tourism_order.order_status >", 2);
        $this->db->where("tourism_order.order_state !=", 5);

        return $this->db->count_all_results($this->table);
    }

    /**
     * @descripte   分润
     * @Author xiumao
     * @date 2016/6/28 0028 下午 3:55
     */
    public function profit_page_list($page_num, $num, $params = array())
    {
        $this->db->select('tourism_order.user_name,
                        tourism_order.order_status,
                        tourism_order.created_at,
                        tourism_order_refund.status,
                        tourism_order_product.number,
                        tourism_order_product.refund_num,
                        tourism_order_product.barter_num,
                        tourism_order_product.shop_price,
                        tourism_order_product.pay_amount,
                        tourism_order_product.goods_name,
                        tourism_order_product_profit.account,
                        tourism_order_product_profit.order_product_id');
        $this->db->join($this->table_3 . ' as tourism_order_product', 'tourism_order_product.order_product_id = tourism_order_product_profit.order_product_id');
        $this->db->join($this->table_2 . ' as tourism_order', 'tourism_order.order_id = tourism_order_product.order_id');
        $this->db->join($this->table_4 . ' as tourism_order_refund', 'tourism_order_refund.order_product_id = tourism_order_product_profit.order_product_id', 'left');

        if ( !empty($params['uid']) ) {
            $this->db->where('tourism_order_product_profit.uid', $params['uid']);
        }
        if ( !empty($params['payer_uid']) ) {
            $this->db->where('tourism_order.payer_uid', $params['payer_uid']);
        }
        if ( !empty($params['order_id']) ) {
            $this->db->where('tourism_order.order_id', $params['order_id']);
        }
        if ( !empty($params['startTime']) ) {
            $this->db->where('tourism_order.created_at >=', $params['startTime'] . ' 00:00:00');
        }
        if ( !empty($params['endTime']) ) {
            $this->db->where('tourism_order.created_at <=', $params['endTime'] . ' 23:59:59');
        }
        if ( !empty($params['status']) && $params['status'] == 2 ) {
            $this->db->where("tourism_order.order_state", 4);
            $this->db->where("tourism_order.order_status", 6);
        } else if ( !empty($params['status']) && $params['status'] == 1 ) {
            $this->db->where("tourism_order.order_status !=", 6);
        }
        $this->db->where("tourism_order.order_status >", 2);//未付款  不显示
        $this->db->where("tourism_order.order_state !=", 5);//退票 不显示
        $this->db->from($this->table);
        $this->db->order_by('tourism_order.created_at', 'DESC');
        $this->db->limit($page_num, $num);

        return $this->db->get();
    }

    /**
     * @descripte   分润已到账收入
     * @Author xiumao
     * @date 2016/6/28 0028 下午 4:55
     */
    public function profit_account($params = array())
    {
        $this->db->select_sum('account');
        $this->db->join($this->table_3 . ' as tourism_order_product', 'tourism_order_product.order_product_id = tourism_order_product_profit.order_product_id');
        $this->db->join($this->table_2 . ' as tourism_order', 'tourism_order.order_id = tourism_order_product.order_id');
        $this->db->join($this->table_4 . ' as tourism_order_refund', 'tourism_order_refund.order_product_id = tourism_order_product_profit.order_product_id', 'left');

        if ( !empty($params['uid']) ) {
            $this->db->where('tourism_order_product_profit.uid', $params['uid']);
        }
        if ( !empty($params['payer_uid']) ) {
            $this->db->where('tourism_order.payer_uid', $params['payer_uid']);
        }
        if ( !empty($params['order_id']) ) {
            $this->db->where('tourism_order.order_id', $params['order_id']);
        }
        if ( !empty($params['startTime']) ) {
            $this->db->where('tourism_order.created_at >=', $params['startTime'] . ' 00:00:00');
        }
        if ( !empty($params['endTime']) ) {
            $this->db->where('tourism_order.created_at <=', $params['endTime'] . ' 23:59:59');
        }
        if ( !empty($params['status']) && $params['status'] == 2 ) {
            $this->db->where("tourism_order.order_state", 4);
            $this->db->where("tourism_order.order_status", 6);
        } else if ( !empty($params['status']) && $params['status'] == 1 ) {
            $this->db->where("tourism_order.order_status !=", 6);
        }
        $this->db->from($this->table);
        $this->db->where("tourism_order.order_status >", 2);
        $this->db->where("tourism_order.order_state !=", 5);

        return $this->db->get();
    }
    
}